package com.boruan.qq.zxgylibrary.ui.adapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.boruan.qq.zxgylibrary.R;
import com.boruan.qq.zxgylibrary.R2;
import com.boruan.qq.zxgylibrary.service.model.ShopCarDataBean;
import com.boruan.qq.zxgylibrary.utils.GlideUtil;
import com.convenient.qd.core.utils.ImageLoader;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 崔琦 on 2019/8/12.
 * des: 商品列表适配器
 */

public class GoodsListAdapter extends RecyclerView.Adapter<GoodsListAdapter.GoodsViewHolder> {

    private Context mContext;
    private List<ShopCarDataBean.ResultBean.ShopCartSkuDTOListBean> mData;
    private GlideUtil glideUtil;

    public GoodsListAdapter(Context mContext) {
        this.mContext = mContext;
        glideUtil = new GlideUtil();
    }

    public void setData(List<ShopCarDataBean.ResultBean.ShopCartSkuDTOListBean> mData){
        this.mData = mData;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public GoodsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.drug_item_goods_list, parent, false);
        return new GoodsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GoodsViewHolder holder, int position) {
        //glideUtil.attach(holder.ivGoodsPic).injectImageWithNull(mData.get(position).getSkuImg());
        ImageLoader.loadImg(mContext,mData.get(position).getSkuImg(),R.mipmap.drug_loading_logo,holder.ivGoodsPic);
        holder.tvGoodsName.setText(mData.get(position).getSkuName());
        holder.tvGoodsNum.setText("x" + mData.get(position).getQuantity());
        double f1 = (double) mData.get(position).getSalePrice() / 100;
        BigDecimal b = new BigDecimal(f1);
        double mSubTotal = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        holder.tvGoodsPrice.setText("￥" + mSubTotal);
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public class GoodsViewHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.iv_goods_pic)
        ImageView ivGoodsPic;
        @BindView(R2.id.tv_goods_name)
        TextView tvGoodsName;
        @BindView(R2.id.tv_goods_num)
        TextView tvGoodsNum;
        @BindView(R2.id.tv_goods_price)
        TextView tvGoodsPrice;
        public GoodsViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
